<?php
/**
 * Users Classes Controller
 *
 * PHP 5.2+
 *
 * Copyright 2012
 * @date 21-April-2012
 * @author
 * @property Auth $Auth
 * @version 0.0.1
 **/
class UsersController extends AppController 
{
    public $components = array("Thumbnail",'Captcha' => array(
                'rotate' => true
            ));    
    public $sN = 'User'; 
    public $pN = 'user';
    public $lN = 'users';    

/**
 * Paginate
 *
 * @var array
 **/        
    /*public $paginate = array(
            'User' => array(
                  'limit'      => '10',
                  'order'   	 => 'User.username'
		      ),
            'Country'=>array(
                'limit'=>30
            ),
            'City'=>array(
                'limit'=>10
            ),
            'Language'=>array(
                'limit'=>10
            )
    );*/
/**
 * beforeFilter callback
 *
 * @return void
 **/
    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel("User");
        $this->loadModel("Group");
        $this->set('sN', $this->sN);        
        $this->set('pN', $this->pN);
        $this->set('lN', $this->lN);        
        //pr($_SESSION);die;
    }


    
/**
 * @method: login
 * @purpose: to provide login
 * @param none
 * @return void
 **/
    public function admin_login() {
        $this->setAdminLogin();
        $this->layout = 'adminlogin';
        if($this->request->is("post")){
            if ($this->Auth->login()) {
                $this->Session->write("Auth.User.indentify","admin");
                $this->redirect(array('controller' => 'users', 'action' => 'index','admin'=>true));
            } else {
                $this->Session->setFlash(__('Invalid username or password, try again'));
            }
        }
    }

/**
 * @method: logout
 * @purpose: logout the user
 * @param none
 * @return void
 **/
    public function logout() {
        $this->autoRender = false;
        $this->Session->destroy();
        $this->Session->setFlash(__('Successfully logged out!', true));                		
        $this->redirect("/admin");
    }
    
    
/**
 * @method: index
 * @purpose: listing of Users
 * @param none
 * @return void
 **/
    public function admin_index() {
        $values = $this->paginate($this->sN,array('User.id <> '.$this->Auth->user('id'),'User.username <> "admin"'));
      	$this->set('values', $values);

    }
    
    public function admin_add() {
        if(!empty($this->data)){
            $this->request->data['User']['created_by']=  $this->Auth->user('id');
            $this->request->data['User']['role_id'] = User::MOD;
            if($this->User->save($this->data)) {
                $this->Session->setFlash(__('Added Successfully', true));                		
                $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true));
            }
        }
        $this->set("groups",$this->Group->get_list());
    }
    public function admin_edit($id = null) {

        if(empty($this->data)) {
            $this->request->data = $this->User->read(null, $id);
        }else{
            $this->User->id = $id;
            if($this->User->save($this->data)) {
                $this->Session->setFlash(__('Updated Successfully', true));
                $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true));
            }
        }
        $this->set('groups',  $this->Group->get_list());
    }

    public function admin_delete($id = null) {
        if(!$id){
            $this->Session->setFlash(__('Invalid user id', true));
            $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true));
        }else{
            $this->User->id = $id;
            if($this->User->delete()) {
                $this->Session->setFlash(__('User has been deleted', true));
                $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true));
            }
        }
    }
    
    // Content  
    
    public function admin_content(){
        $this->loadModel("Content");
        $this->set('values',  $this->Content->find('all'));
    }        
    public function admin_content_images($content_id=null) {
        $this->loadModel("Content");
        $this->loadModel("ContentImage");
        $content = $this->Content->read(null,$content_id);
        if(!empty($this->data)){
            //pr($this->data);die;
            if(!empty($this->data['Content']['image'])){
                $time = time();
                $i=1;
                foreach ($this->data['Content']['image'] as $image) {
                    if($image['name']){
                        $img_name = $time.$i.$image['name'];
                        $this->ContentImage->create();
                        $this->ContentImage->save(array('content_id'=>$content_id,'image'=>$img_name));
                        $img_source  = WWW_ROOT.'img/contents/'.$img_name;
                        if(move_uploaded_file($image['tmp_name'],$img_source)){
                            $more_img[]=$time.$i.$image['name'];
                            $source_fille=WWW_ROOT.'img/contents/'.$img_name;
                            $source_save=WWW_ROOT.'img/contents/thumb/'.$img_name;                
                            $this->Thumbnail->Thumbnail($source_fille);
                            $this->Thumbnail->size_height(140);
                            $this->Thumbnail->size_width(140);
                            $this->Thumbnail->save($source_save);
                        }
                    }
                    $i++;
                }
            }
            $this->Session->setFlash("The images has been saved");
        }
        $content_images = $this->ContentImage->get_images($content_id);
        $this->set('images',$content_images);
        $this->set("content",$content);
        $this->request->data= $this->Content->read(null,$content_id);
    }
    public function admin_content_edit($content_id=null) {
        $this->loadModel("Content");
        $this->loadModel("ContentImage");
        $content = $this->Content->read(null,$content_id);
        if(!empty($this->data)){
            $time = time();
            if(isset($this->data['Content']['main_image'])&&$this->data['Content']['main_image']['name']){                
                if(move_uploaded_file($this->data['Content']['main_image']['tmp_name'],WWW_ROOT.'img/contents/'.$time.$this->data['Content']['main_image']['name'])){
                    $this->request->data['Content']['main_image']=$time.$this->data['Content']['main_image']['name'];
                }
                $source_fille=WWW_ROOT.'img/contents/'.$this->data['Content']['main_image'];
                $source_save=WWW_ROOT.'img/contents/thumb/'.$this->data['Content']['main_image'];                
                $this->Thumbnail->Thumbnail($source_fille);
                $this->Thumbnail->size_height(140 );
                $this->Thumbnail->size_width(140);
                $this->Thumbnail->save($source_save);
                if($content['Content']['main_image']){
                    unlink(WWW_ROOT.'img/contents/'.$content['Content']['main_image']);
                    unlink(WWW_ROOT.'img/contents/thumb/'.$content['Content']['main_image']);
                }
            }else{
                $this->request->data['Content']['main_image']=$content['Content']['main_image'];
            }
            $this->Content->id = $content_id;
            $this->Content->save($this->data);
            $this->Session->setFlash("The content has been saved");
            $this->redirect(array('action'=>'content'));
        }
        $content_images = $this->ContentImage->get_images($content_id);
        $this->set('images',$content_images);
        $this->request->data= $this->Content->read(null,$content_id);
        //main_image
    }

    // Group
    public function admin_content_delete_image($image_id){
        $this->autoRender = false;
        $this->loadModel("ContentImage");
        $this->ContentImage->id = $image_id;
        $img_name = $this->ContentImage->field('image');
        if($this->ContentImage->delete()){
            unlink(WWW_ROOT.'img/contents/'.$img_name);
            unlink(WWW_ROOT.'img/contents/thumb/'.$img_name);
            return 'true';
        }
        return 'false';
    }
    public function admin_group(){
        $this->loadModel("Group");
        $this->set('values',  $this->Group->find('all'));
    }
    public function admin_group_delete($id = null) {
        $this->loadModel("Group");
        if(!$id){
            $this->Session->setFlash(__('Invalid group id', true));
            $this->redirect(array('controller' => 'users', 'action' => 'group', 'admin' => true));
        }else{
            $this->Group->id = $id;
            if($this->Group->delete()) {
                $this->User->deleteAll(array('group_id'=>$id));
                $this->Session->setFlash(__('Group has been deleted', true));
                $this->redirect(array('controller' => 'users', 'action' => 'group', 'admin' => true));
            }
        }
    }
    public function admin_group_add(){
        $this->loadModel("Group");
        if(!empty($this->data)) {
            if(isset($this->data['Group']['data']) && !empty($this->data['Group']['data'])){
                if(!empty($this->data['Group']['data'])){
                    foreach($this->data['Group']['data'] as $controller=>$actions){
                        if(in_array($controller, array('Courses','OnsiteSesions','OpenSesions','Discounts','Trainers','Users'))){
                            if(!empty($actions)){
                                if(!in_array('index', $actions)){
                                    $this->data['Group']['data'][$controller][] = 'index'; 
                                }
                            }
                        }
                    }
                }
                $this->request->data['Group']['data'] = json_encode($this->data['Group']['data']);
            }
            $this->request->data['Group']['created_by']=  $this->Auth->user('id');
            if($this->Group->save($this->data)) {
                $this->Session->setFlash(__('Added Successfully', true));                		
                $this->redirect(array('controller' => 'users', 'action' => 'group', 'admin' => true));
            }	            
        }
        $controllers=$this->UserAccess->getListController();
        $this->set('controllers',$controllers);
    }
    public function admin_group_edit($id=null){
        if(empty($this->data)){
            $this->request->data = $this->Group->read(null,$id);
            $this->request->data['Group']['data'] = json_decode($this->data['Group']['data'], true);
        }elseif(!empty($this->data)) {
            if(isset($this->data['Group']['data']) && !empty($this->data['Group']['data'])){
                if(!empty($this->data['Group']['data'])){
                    foreach($this->data['Group']['data'] as $controller=>$actions){
                        if(in_array($controller, array('Courses','OnsiteSesions','OpenSesions','Discounts','Trainers','Users'))){
                            if(!empty($actions)){
                                if(!in_array('index', $actions)){
                                    $this->data['Group']['data'][$controller][] = 'index'; 
                                }
                            }
                        }
                    }
                }
                $this->request->data['Group']['data'] = json_encode($this->data['Group']['data']);
            }
            $this->Group->id = $id;
            if($this->Group->save($this->data)) {
                $this->Session->setFlash(__('Editted Successfully', true));                		
                $this->redirect(array('controller' => 'users', 'action' => 'group', 'admin' => true));
            }	            
        }
        $controllers=$this->UserAccess->getListController();
        $this->set('controllers',$controllers);
    }
    public function admin_changepass($id=null){
        if(empty($this->data)) {
            $this->request->data = $this->User->read(null, $id);
        } else {
            $this->User->id = $id;
            if($this->User->save($this->data)) {
                $this->Session->setFlash(__('Updated Successfully', true));
                $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true));
            }
        }
    }
    
    
    public function admin_setting() {
         $this->loadModel('Setting');
         if ($this->request->is('post')) { 
                $options = $this->request->data['Setting'];
                $valid = 1;
                foreach($options as $option => $value){
                    $opt = $this->Setting->findByName($option);
                    if(!empty($opt)){
                        $this->Setting->id = $opt['Setting']['id'];
                        $this->Setting->saveField('value',$value);
                    }
                }
            $this->Session->setFlash('Your setting has been saved.');
         }
    }
    public function admin_sliders(){
        $this->loadModel("Slider");
        if(!empty($this->data)){
            $_POST = $this->data['Slider'];
            $image = "";
            $time = time();
            if(isset($_POST['image'])&&$_POST['image']['name']){                
                if(move_uploaded_file($_POST['image']['tmp_name'],'img/Sliders/'.$time.$_POST['image']['name'])){
                    $image=$time.$_POST['image']['name'];
                }
                $source_fille=WWW_ROOT.'img/Sliders/'.$image;
                $source_save=WWW_ROOT.'img/Sliders/thumb/'.$image;                
                $this->Thumbnail->Thumbnail($source_fille);
                $this->Thumbnail->size_height(300 );
                $this->Thumbnail->size_width(300);
                $this->Thumbnail->save($source_save);
            }
            if($image !=""){
                $this->Slider->create();
                $this->Slider->save(array('image'=>$image,'image_url'=>$_POST['image_url']));
            }
        }
        $this->set("values",  $this->Slider->find('all'));
    }
    public function admin_slider_delete($slider_id){
        $this->loadModel('Slider');
        $this->Slider->id = $slider_id;
        $image = $this->Slider->field('image');
        $this->Slider->delete();
        @unlink(WWW_ROOT.'img/Sliders/'.$image);
        @unlink(WWW_ROOT.'img/Sliders/thumb/'.$image);
        $this->Session->setFlash('The slider image has been deleted!');
        $this->redirect(array('controller'=>'users','action'=>'sliders'));
    }
    public function captcha()  {
        $this->autoRender = false;
        $this->Captcha->generate();
    }
    
    public function verification() {
        $this->layout = 'default';
        
    }
}